package com.qixin.api.serviceimpl;

import com.qixin.api.bean.ComFile;
import com.qixin.api.bean.FileResult;
import com.qixin.api.bean.ResultPage;
import com.qixin.api.bean.VoPage;
import com.qixin.api.mapper.ComFileMapper;
import com.qixin.api.service.ComFileService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.util.file.upload.UploadFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.*;

/**
 * <p>
 * 公用 文件表 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-03
 */
@Service
public class ComFileServiceImpl extends ServiceImpl<ComFileMapper, ComFile> implements ComFileService {
    private static final Logger logger = LoggerFactory.getLogger(ComFileServiceImpl.class);

    @Override
    public FileResult upload(MultipartFile file, ComFile comFile) {
        logger.info("文件名字->"+file.getOriginalFilename());
        String url="";
        try {
            url=  UploadFile.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        logger.info("文件上传完毕后的路径->"+url);
        comFile.setFileName(file.getOriginalFilename());
        comFile.setUrl(url);
        comFile.setCreateTime(new Date().getTime());
        FileResult result=new FileResult();
        //上传成功 返回数据 返回本次上传的文件ID
        if(this.insert(comFile)){
            result.setCode(0);
            result.setMsg("上传成功");
            Map map=new HashMap<>();
            map.put("fileId",comFile.getId());
            map.put("url",url);
            result.setData(map);
            return  result;
        };
        result.setCode(1);
        result.setMsg("上传失败");
        return  result;
    }
    @Override
    public List<ComFile> selectphoto(Long id) {
        List<ComFile> list = baseMapper.selectphoto(id);

        return list;
    }
    @Override
    public List<ComFile> selectpa(Long id) {
        List<ComFile> list = baseMapper.selectpa(id);

        return list;
    }

    //查询出来comFile表中relation_id是空的
    @Override
    public List<ComFile> selectnull() {
        return baseMapper.selectnull();
    }

    //删除
    @Override
    public boolean deleteRelationId() {
        return baseMapper.deleteRelationId();
    }
}
